package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQUEST = 1001;
    String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.CALL_PHONE, Manifest.permission.READ_EXTERNAL_STORAGE};
    List<String> permissionsList = new ArrayList<>();
    public static final int TAKE_PHOTO = 1;
    private ImageView picture;
    private Uri imageUri;
    private List<Photo> photoList = new ArrayList<>();
    private TextView pt;
    private int w = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPermissions();
        Button takePhoto = (Button) findViewById(R.id.take_photo);
        ImageAdapter adapter = new ImageAdapter(MainActivity.this, R.layout.image_item, photoList);
        picture = (ImageView) findViewById(R.id.picture);
        pt = (TextView) findViewById(R.id.photo_time);
        //       ListView listView = (ListView) findViewById(R.id.list_item);
        //       listView.setAdapter(adapter);
        takePhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
        /*        File outPutImage = new File(getExternalCacheDir(), "output_image.jpg");
              /*  try {
                   if (outPutImage.exists()) {
                        outPutImage.delete();
                    }
                    outPutImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
            /*    if (Build.VERSION.SDK_INT >= 23) {
                    imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", outPutImage);
                } else {
                    imageUri = Uri.fromFile(outPutImage);
                }*/
                Intent intent = new Intent(MainActivity.this, CameraActivity.class);
                //       intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, 1);


            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (requestCode != RESULT_OK) {
                    String A = data.getStringExtra("data_return");
                    File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera/" + w + ".jpg");
                    Long fileTime = file.lastModified();
                    Date date = new Date(fileTime);
                    SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy年MM月dd日");
                    simpleDateFormat1.format(date);
                  //  Toast.makeText(MainActivity.this, w + "", Toast.LENGTH_SHORT).show();
                    pt.setText(date.toString());
                    File path = Environment.getExternalStorageDirectory();
                     //获得SDCard目录
                    Bitmap bmpDefaultPic = BitmapFactory.decodeFile(path + "/DCIM/Camera/" + w + ".jpg", null);
                    picture.setImageBitmap(bmpDefaultPic);
              //       Toast.makeText(MainActivity.this, "ha", Toast.LENGTH_SHORT).show();


 /*                   try {
                        Uri myUri = Uri.parse(data.getStringExtra("data_return"));

                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(myUri));
                        //  Photo a=new Photo(getPhotoFileName(),R.drawable.ic_launcher_foreground);
                        //    photoList.add(a);
                        picture.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }*/
                }
                break;
            default:
                break;
        }
    }


    private void initPermissions() {
        permissionsList.clear();

        //判断哪些权限未授予
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
                permissionsList.add(permission);
            }
        }

        //请求权限
        if (!permissionsList.isEmpty()) {
            String[] permissions = permissionsList.toArray(new String[permissionsList.size()]);//将List转为数组
            ActivityCompat.requestPermissions(MainActivity.this, permissions, PERMISSION_REQUEST);
        }
    }


    private String getPhotoFileName() {
// TODO Auto-generated method stub
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat(
                "'IMG'_yyyyMMdd_HHmmss");
        return dateFormat.format(date) + ".jpg";
    }


}